package com.think.teaching.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.think.teaching.model.News;
import com.think.teaching.model.Tag;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;


/**
 * <p>
 * 文章Mapper 接口
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
//@Repository
public interface NewsMapper extends BaseMapper<News>{

	@Select(value = "SELECT * FROM news WHERE delflag=0 ORDER BY updated_time DESC")
	@Results({
		@Result(column="id",property="id"),
		@Result(column="title",property="title"),
		@Result(column="author",property="author"),
		@Result(column="image",property="image"),
		@Result(column="updated_time",property="updatedTime"),
		@Result(column="content",property="content"),
		@Result(column="state",property="state"),
		@Result(column="id",property="tags",
		one=@One(select="com.think.teaching.mapper.NewsMapper.getNewsTagById",fetchType= FetchType.EAGER)),
		@Result(column="id",property="tagIds",
		one=@One(select="com.think.teaching.mapper.NewsMapper.getNewsTagIdsById",fetchType= FetchType.EAGER)),
		@Result(column="type",property="typeInfo",
		one=@One(select="com.think.teaching.mapper.NewsTypeMapper.getInfoById",fetchType= FetchType.EAGER))
	})
	IPage<News> getNewsList(IPage<News> page);
	
	
	@Select("SELECT tag.id,tag.tag,tag.type,tag.description FROM tag WHERE id IN(SELECT news_tag.tag_id FROM news_tag WHERE news_id=#{id})")
	List<Tag> getNewsTagById(Integer id);
	
	@Select("SELECT news_tag.tag_id FROM news_tag WHERE news_tag.news_id=#{id}")
	List<Integer> getNewsTagIdsById(Integer id);
	
	@Select("<script>" +
			"SELECT * FROM news WHERE delflag=0 AND id=#{id} " +
			"<if test='state gt 0'> AND state =#{state}</if>"
			+
			"</script>"
	)
	@Results({
		@Result(column="id",property="id"),
		@Result(column="title",property="title"),
		@Result(column="author",property="author"),
		@Result(column="image",property="image"),
		@Result(column="updated_time",property="updatedTime"),
		@Result(column="content",property="content"),
		@Result(column="state",property="state"),
		@Result(column="id",property="tags",
		one=@One(select="com.think.teaching.mapper.NewsMapper.getNewsTagById",fetchType= FetchType.EAGER)),
		@Result(column="id",property="tagIds",
		one=@One(select="com.think.teaching.mapper.NewsMapper.getNewsTagIdsById",fetchType= FetchType.EAGER)),
		@Result(column="type",property="typeInfo",
		one=@One(select="com.think.teaching.mapper.NewsTypeMapper.getInfoById",fetchType= FetchType.EAGER))
	})
	News getNewsDetailes(
			@Param("id") Integer id,
			@Param("state") Integer state);
	
	@Select("<script>" +
			"SELECT * FROM news WHERE delflag=0 " +
			"<if test='type gt 0'>AND type=#{type}</if>"
			+
			"<if test='state gt 0'> AND state =#{state}</if>"
			+
			"<if test='type gt 0'> AND type = #{type}</if>"
			+
			"<if test='tag gt 0'> AND id IN(SELECT news_tag.news_id FROM news_tag WHERE tag_id=#{tag})</if>"
			+
			"ORDER BY updated_time DESC"
			+
			"</script>"
	)
	@Results({
		@Result(column="id",property="id"),
		@Result(column="title",property="title"),
		@Result(column="author",property="author"),
		@Result(column="image",property="image"),
		@Result(column="content",property="content"),
		@Result(column="state",property="state"),
		@Result(column="id",property="tags",
		one=@One(select="com.think.teaching.mapper.NewsMapper.getNewsTagById",fetchType= FetchType.EAGER)),
		@Result(column="id",property="tagIds",
		one=@One(select="com.think.teaching.mapper.NewsMapper.getNewsTagIdsById",fetchType= FetchType.EAGER)),
		@Result(column="type",property="typeInfo",
		one=@One(select="com.think.teaching.mapper.NewsTypeMapper.getInfoById",fetchType= FetchType.EAGER))
	})
	IPage<News> getNewsListWithFilter(IPage<News> page,
									 @Param("type") Integer type,
									  @Param("tag") Integer tag,
									  @Param("state") Integer state,
									  @Param("dataType") Integer dataType);
	
	@Select("SELECT count(*) FROM news where title=#{title}")
	int getSameTitleNewsNums(String title);
}
